package com.geekstudy.week1.tag;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class CommonResponseHeaderTag extends SimpleTagSupport {

    // final String CacheControl = "Cache-Control";

    private String cacheControl;

    private String pragma;

    private Long expires;

    public void setCacheControl(String cacheControl) {
        this.cacheControl = cacheControl;
    }

    public void setPragma(String pragma) {
        this.pragma = pragma;
    }

    public void setExpires(Long expires) {
        this.expires = expires;
    }

    @Override
    public void doTag() throws JspException, IOException {
        System.out.println("CommonResponseHeaderTag start");
        JspContext jspContext = getJspContext();
        PageContext pageContext = (PageContext)jspContext;

        // HttpServletRequest httpRequest = (HttpServletRequest)pageContext.getRequest();

        // String cacheControlValue = (String)httpRequest.getAttribute(CacheControl);
        // System.out.println("CommonResponseHeaderTag " + CacheControl + " " + cacheControlValue);
        System.out.println("ComonResponseHeaderTag cacheControl:" + cacheControl + "  pragma:" + pragma + "  expires:" + expires);



        HttpServletResponse httpResponse = (HttpServletResponse)pageContext.getResponse();
        //httpResponse.setHeader("Cache-Control", "no-cache");
        //httpResponse.setHeader("Pragma", "no-cache");
        //httpResponse.setDateHeader("Expires", -1);
        httpResponse.setHeader("Cache-Control", cacheControl);
        httpResponse.setHeader("Pragma", pragma);
        httpResponse.setHeader("Expires", String.valueOf(expires));


        System.out.println("CommonResponseHeaderTag finish");

    }
}
